package com.bdqn.controller;

import com.alibaba.fastjson.JSON;
import com.bdqn.pojo.StorageRecord;
import com.bdqn.pojo.Supplier;
import com.bdqn.service.StorageRecordService;
import com.bdqn.service.SupplierService;
import com.bdqn.utils.Constants;
import com.bdqn.utils.PageSupport;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
    @Controller
    @RequestMapping("/storageRecord") // 路径
    public class StorageRecordController {
        @Resource
        private StorageRecordService storageRecordService;
        @Resource
        private SupplierService supplierService;
        @GetMapping("/list")
        public String toList(Model model,String queryGoodsName,Integer querySupplierId,Integer queryPayStatus,@RequestParam(defaultValue = "1")Integer pageIndex){
            List<StorageRecord> storageRecords = null;
            List<Supplier> supplierList = null;
            try{
                //设置页面容量
                int pageSize = Constants.pageSize;
                //总数量
                int totalCount = storageRecordService.getCount();
                //总页数
                PageSupport pages = new PageSupport();
                pages.setCurrentPageNo(pageIndex); // 设置当前页数
                pages.setPageSize(pageSize);       // 设置一页显示数据量
                pages.setTotalCount(totalCount);   // 设置总数据量
                int totalPageCount = pages.getTotalPageCount();
                //控制首页和尾页
                if (pageIndex > totalPageCount){
                    pageIndex = totalPageCount;
                    throw new RuntimeException("页码不正确");
                }
                // 分页查询方法
                storageRecords = storageRecordService.getStorageRecordList((pageIndex-1)*pageSize,pageSize,queryGoodsName,querySupplierId,queryPayStatus);
                // 调用方法查询所有供货商信息
                supplierList = supplierService.getAll();
                // 将数据放入model中
                model.addAttribute("storageRecordList",storageRecords);  // 用户信息
                model.addAttribute("totalCount",pages.getTotalCount()); // 分页信息
                model.addAttribute("currentPageNo",pages.getPageNo());
                model.addAttribute("totalPageCount",pages.getTotalPageCount());
                model.addAttribute("queryGoodsName",queryGoodsName); // 查询条件回显
                model.addAttribute("querySupplierId",querySupplierId);
                model.addAttribute("queryPayStatus",queryPayStatus);
                model.addAttribute("supplierList",supplierList);    // 供货商数据
            }catch (Exception e){
                e.printStackTrace();
            }
            return "/storageRecord/list";
        }
        @PostMapping("/add")
        public String doAdd(Model model,String srCode,String goodsName,double goodsCount,String goodsUnit,double totalAmount,Integer supplierId,Integer payStatus){
            StorageRecord storageRecord = new StorageRecord();
            storageRecord.setSrCode(srCode);
            storageRecord.setGoodsName(goodsName);
            storageRecord.setGoodsCount(goodsCount);
            storageRecord.setGoodsUnit(goodsUnit);
            storageRecord.setTotalAmount(totalAmount);
            storageRecord.setSupplierId(supplierId);
            storageRecord.setPayStatus(payStatus);
            // 调用添加方法
            int add = storageRecordService.add(storageRecord);
            if (add == 1){
                return "redirect:/storageRecord/list";
            }
            return "/storageRecord/add";
        }
        // 跳转到添加页面
        @GetMapping("/toAdd")
        public String add(Model model){
            // 查询供货商的名称放到下拉框中
            List<Supplier> all = supplierService.getAll();
            model.addAttribute("supplier",all);
            return "/storageRecord/add";
        }

        @RequestMapping("/view/{storageRecordId}")
        public String view(Model model,@PathVariable Integer storageRecordId ){
            StorageRecord storageRecord = storageRecordService.getStorageRecord(storageRecordId);
            model.addAttribute("storageRecord",storageRecord);
            return "/storageRecord/view";
        }
        @ResponseBody
        @RequestMapping("/del/{storageRecordId}")
        public String del(@PathVariable Integer storageRecordId){
            Map<String,String> map = new HashMap<>();
            int del = storageRecordService.del(storageRecordId);
            if (del == 1){
                map.put("delResult","true");
            }else{
                map.put("delResult","false");
            }
            return JSON.toJSONString(map);
        }

        @RequestMapping("/toUpdate/{storageRecordId}")
        public String toUpdate(Model model,@PathVariable Integer storageRecordId){
            // 根据id查询出要修改的信息
            StorageRecord storageRecord = storageRecordService.getStorageRecord(storageRecordId);
            model.addAttribute("storageRecord",storageRecord);
            // 查询所用供货商信息
            List<Supplier> all = supplierService.getAll();
            model.addAttribute("supplierList",all);
            model.addAttribute("id",storageRecordId);
            return "/storageRecord/update";
        }

        @PostMapping("/update")
        public String update(Model model,Integer id,String srCode,String goodsName,double goodsCount,String goodsUnit,double totalAmount,Integer supplierId,Integer payStatus){
            StorageRecord storageRecord = new StorageRecord();
            storageRecord.setId(id);
            storageRecord.setSrCode(srCode);
            storageRecord.setGoodsName(goodsName);
            storageRecord.setGoodsCount(goodsCount);
            storageRecord.setGoodsUnit(goodsUnit);
            storageRecord.setTotalAmount(totalAmount);
            storageRecord.setSupplierId(supplierId);
            storageRecord.setPayStatus(payStatus);
            // 调用修改方法
            int upd = storageRecordService.upd(storageRecord);
            if (upd == 1){
                return "redirect:/storageRecord/list";
            }
            return "/storageRecord/update";
        }
    }
